package com.china08.yxyapi.api.manage.tec.teaching.lecture.res;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.EsResourcePvUpdateUtil;
import com.china08.yxyapi.util.EsResourceUpdateUtil;
import com.china08.yxyapi.util.YxyPlayUrlForWeiKechengBuilder;
import com.china08.yxyapi.util.YxyResUtil;

/**
 * 获取资源查看接口
 */
@CrossOrigin
@RestController
public class ResResourcePreViewController extends YxyApiBaseController {

    @Autowired
    private YxyResourceService ysr;

    @Autowired
    private YxyResUtil yxyresutil;

    @Autowired
    private YxyResResourceRepository yrrr;

    @Autowired
    YxyPlayUrlForWeiKechengBuilder builder;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/res/preview/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "id") String id) {

        YxyProfile profile = getProfile(request, response);
        YxyResResource resource = yrrr.findById(id);
        Assert.notNull(resource, "资源找不到了");
        EsResourcePvUpdateUtil.updateEsResoure(id + "_1");
        resource.setPv(resource.getPv() + 1);
        yrrr.save(resource);
        // 通知ES更改资源的变更
        EsResourceUpdateUtil.updateEsResoure(resource.getId());

        RespModel model = new RespModel();
        model.title = resource.getTitle();
        model.formatId = resource.getFormatId();
        String url = "";
        if (resource.getTypeId().equals("yx14") || resource.getFormatId().equals("FMT04")) {
            url = builder.setResourceId(id).setUserId(profile.getId()).setUserVip(Boolean.valueOf(profile.getUserVip()))
                    .setResVip(Boolean.valueOf(profile.getResVip())).setViewFrom("weikecheng").build();
        } else {
            url = yxyresutil.getPreviewUrl(id);
        }
        // respModel.location = url;
        model.preview = url;
        model.downloadLocation = yxyresutil.getDownloadUrl(id);
        String extension = resource.getExtension();
        model.examine = url;
        return model;
    }

    protected static class RespModel {
        public String title;
        public String formatId;
        public String preview;
        public String downloadLocation;
        public String examine;
    }

}
